home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Mar / DI9803DB / common / CompStream.pas next >
Encoding:
Pascal/Delphi Source File  |  1997-08-04  |  2.5 KB  |  106 lines

  1. unit CompStream;
  2. {
  3.   Delphi component streaming unit
  4.   Author:              David W. Body / Big Creek Software
  5.   E-mail address:      davidbody@bigcreek.com
  6.   Original Version:    August 4, 1997
  7.   Last Updated:        August 4, 1997
  8.   This code is hereby placed in the public domain.
  9. }
  10.  
  11. interface
  12.  
  13. uses Classes;
  14.  
  15. function ComponentToString(Component: TComponent): string;
  16. function StringToComponent(Value: string): TComponent;
  17. function ComponentToVariant(Component: TComponent): Variant;
  18. function VariantToComponent(Value: Variant): TComponent;
  19.  
  20. implementation
  21.  
  22. function ComponentToString(Component: TComponent): string;
  23. var
  24.   BinStream: TMemoryStream;
  25.   StrStream: TStringStream;
  26.   s: string;
  27. begin
  28.   BinStream := TMemoryStream.Create;
  29.   try
  30.     StrStream := TStringStream.Create(s);
  31.     try
  32.       BinStream.WriteComponent(Component);
  33.       BinStream.Seek(0, soFromBeginning);
  34.       ObjectBinaryToText(BinStream, StrStream);
  35.       StrStream.Seek(0, soFromBeginning);
  36.       Result := StrStream.DataString;
  37.     finally
  38.       StrStream.Free;
  39.     end;
  40.   finally
  41.     BinStream.Free;
  42.   end;
  43. end;
  44.  
  45. function StringToComponent(Value: string): TComponent;
  46. var
  47.   StrStream: TStringStream;
  48.   BinStream: TMemoryStream;
  49. begin
  50.   StrStream := TStringStream.Create(Value);
  51.   try
  52.     BinStream := TMemoryStream.Create;
  53.     try
  54.       ObjectTextToBinary(StrStream, BinStream);
  55.       BinStream.Seek(0, soFromBeginning);
  56.       Result := BinStream.ReadComponent(nil);
  57.     finally
  58.       BinStream.Free;
  59.     end;
  60.   finally
  61.     StrStream.Free;
  62.   end;
  63. end;
  64.  
  65. function ComponentToVariant(Component: TComponent): Variant;
  66. var
  67.   BinStream: TMemoryStream;
  68.   Data: Pointer;
  69. begin
  70.   BinStream := TMemoryStream.Create;
  71.   try
  72.     BinStream.WriteComponent(Component);
  73.     Result := VarArrayCreate([0, BinStream.Size - 1], varByte);
  74.     Data := VarArrayLock(Result);
  75.     try
  76.       Move(BinStream.Memory^, Data^, BinStream.Size);
  77.     finally
  78.       VarArrayUnlock(Result);
  79.     end;
  80.   finally
  81.     BinStream.Free;
  82.   end;
  83. end;
  84.  
  85. function VariantToComponent(Value: Variant): TComponent;
  86. var
  87.   BinStream: TMemoryStream;
  88.   Data: Pointer;
  89. begin
  90.   BinStream := TMemoryStream.Create;
  91.   try
  92.     Data := VarArrayLock(Value);
  93.     try
  94.       BinStream.WriteBuffer(Data^, VarArrayHighBound(Value, 1) + 1);
  95.     finally
  96.       VarArrayUnlock(Value);
  97.     end;
  98.     BinStream.Seek(0, soFromBeginning);
  99.     Result := BinStream.ReadComponent(nil);
  100.   finally
  101.     BinStream.Free;
  102.   end;
  103. end;
  104.  
  105. end.
  106.